package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

public class LoginPage extends Utils {
    private static String URL = "http://113.44.92.31:65533/sign-in.html";
    public LoginPage() {
        super(URL);
    }

    /**
     * 登录页面 页面元素检查
     */
    public void checkLoginElements() {
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2"));
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.xpath("//*[@id=\"password\"]"));
        driver.findElement(By.id("submit"));
        driver.findElement(By.cssSelector("#signInForm > div.mb-2 > label > span > a")).getText().equals("忘记密码");
        driver.findElement(By.cssSelector("#password_a > svg"));
        driver.findElement(By.cssSelector("#username")).click();
        driver.findElement(By.cssSelector("#password")).click();
        driver.findElement(By.cssSelector("#signInForm > div.mb-3 > div")).getText().equals("用户名不能为空");
        driver.findElement(By.cssSelector("#signInForm > div.mb-2 > div > div")).getText().equals("密码不能为空");
    }

    /**
     * 测试跳转注册界面和重置密码界面
     */
    public void checkJump() throws InterruptedException {
        //点击忘记密码，跳转忘记密码页面

        JavascriptExecutor js = (JavascriptExecutor) driver;
        element = driver.findElement(By.cssSelector("#signInForm > div.mb-2 > label > span > a"));
        js.executeScript("arguments[0].click();",element);
        //driver.findElement(By.cssSelector("#signInForm > div.mb-2 > label > span > a")).click();

        String afterURL = driver.getCurrentUrl();
        assert afterURL.equals("http://113.44.92.31:65533/forgot-password.html");
        //点击重置密码界面的返回登录，调回登录页面
        driver.findElement(By.cssSelector("body > div > div > div.text-center.text-muted.mt-3 > a")).click();
        afterURL = driver.getCurrentUrl();
        assert afterURL.equals(URL);
        //点击注册，跳转注册界面
        driver.findElement(By.cssSelector("body > div.page.page-center > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a")).click();
        afterURL = driver.getCurrentUrl();
        assert afterURL.equals("http://113.44.92.31:65533/sign-up.html");
        //使用 JavaScript 直接点击（绕过 Selenium 的交互检查）
        element = driver.findElement(By.cssSelector("body > div > div > div.text-center.text-muted.mt-3 > a"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

        afterURL = driver.getCurrentUrl();
        assert afterURL.equals(URL);
    }

    /**
     * 登录成功
     * @throws InterruptedException
     */
    public void loginSuccess() throws InterruptedException {
        driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
        driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
        driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys("Cyan");
        driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("1111");
        Thread.sleep(1000);
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        Thread.sleep(1000);
        String afterURL = driver.getCurrentUrl();
        assert !afterURL.equals(URL);
    }


    /**
     * 登录失败 - 用户名正确，密码错误
     */
    public void loginFail() {
        driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
        driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
        driver.findElement(By.cssSelector("#username")).sendKeys("Cyan");
        driver.findElement(By.cssSelector("#password")).sendKeys("Cyan");
        driver.findElement(By.cssSelector("#submit")).click();
        String afterURL = driver.getCurrentUrl();
        assert afterURL.equals(URL);
    }
    /**
     * 登录失败 - 用户名错误，密码正确
     * @throws InterruptedException
     */
    public void loginFail2() throws InterruptedException {
        driver.findElement(By.xpath("//*[@id=\"username\"]")).clear();
        driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
        driver.findElement(By.cssSelector("#username")).sendKeys("C");
        driver.findElement(By.cssSelector("#password")).sendKeys("0901");
//        Thread.sleep(1000);
        driver.findElement(By.cssSelector("#submit")).click();
        String afterURL = driver.getCurrentUrl();
        assert afterURL.equals(URL);
    }
}
